package com.yhq.spring.event.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.support.TaskUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hqyin
 * @date 2023/3/13 3:08 下午
 */
@RestController
@Slf4j
public class ExEventController {
    @Autowired
    private ApplicationContext applicationContext;



    @GetMapping("/ex/event/{msgId}")
    public String pushEvent(@PathVariable String msgId){
        ExEvent event = new ExEvent();
        event.setMsgId(msgId);
        event.setType("add");
        SimpleApplicationEventMulticaster applicationEventMulticaster = (SimpleApplicationEventMulticaster) applicationContext.getBean("applicationEventMulticaster");
        applicationEventMulticaster.setErrorHandler(new MyErrorHandler());

        applicationContext.publishEvent(event);
        log.info("publish ex event");
        return "success";
    }
}
